﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace DeadlockDetection
{ 
    public class Processes
    {
        public Process[] processes;
        private Random random = new Random();
        public int Length { get { return processes.Length; } }

        public Processes(int numberOfProcesses, Resources resources)
        {
            processes = new Process[numberOfProcesses];

            for (int i = 0; i < numberOfProcesses; i++)
            {
                processes[i] = new Process(i, resources);
            }
        }

        public Processes(int numberOfProcesses, Resources resources, bool single)
        {
            processes = new Process[numberOfProcesses];

            for (int i = 0; i < numberOfProcesses; i++)
            {
                processes[i] = new Process(i, resources, single);
            }
        }

        public Process this[int x]
        {
            get { return processes[x]; }
        }

        // wczytywanie instancji problemu z pliku
        public Processes(StreamReader instanceFile, int numberOfProcesses)
        {
            string buf = " ";
            while(!buf.StartsWith("processes"))
            {
                buf = instanceFile.ReadLine();    
            }

            processes = new Process[numberOfProcesses];

            for (int i = 0; i < numberOfProcesses; i++)
            {
                buf = instanceFile.ReadLine();
                buf.Trim();
                string[] neededFields = buf.Split(' ');

                buf = instanceFile.ReadLine();
                buf.Trim();
                string[] allocatedFields = buf.Split(' ');

                buf = instanceFile.ReadLine();
                buf.Trim();
                string[] requestedFields = buf.Split(' ');

                int[] needed = new int[neededFields.Length];
                int[] allocated = new int[allocatedFields.Length];
                int[] requested = new int[requestedFields.Length];

                for (int j = 0; j < neededFields.Length; j++)
                {
                    needed[j] = Convert.ToInt32(neededFields[j]);
                    allocated[j] = Convert.ToInt32(allocatedFields[j]);
                    requested[j] = Convert.ToInt32(requestedFields[j]);
                }
                
                processes[i] = new Process(i, needed, allocated, requested);

                instanceFile.ReadLine();
            }
        }
    }
}
